package com.mes.inventory.dao;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.bstek.dorado.data.provider.Page;
import com.mes.common.dao.BasicDao;
import com.mes.inventory.dao.iface.IWarebinKeeperDao;
import com.mes.inventory.domain.WarebinKeeper;
import com.mes.inventory.domain.WarehouseKeeper;

@Repository
public class WarebinKeeperDao extends BasicDao<WarebinKeeper> implements IWarebinKeeperDao{

	@Override
	public void getWarebinKeeper(Page<WarebinKeeper> page, String warehouseUid) throws Exception {
		// TODO Auto-generated method stub
		Map<String, Object> args = new HashMap<String, Object>();
		StringBuffer coreHql = new StringBuffer("from "
				+ WarebinKeeper.class.getName() + " a where 1=1 ");

		if (StringUtils.isNotEmpty(warehouseUid)) {
			coreHql.append(" and a.warehouseUid =:warehouseUid ");
			args.put("warehouseUid", warehouseUid);
		}
		String countHql = "select count(*) " + coreHql.toString();
		String hql = coreHql.toString();
		
		Map<String, Object> newArgs = new HashMap<String, Object>();
		String key;
		String newKey;
		Iterator<String> iterator = args.keySet().iterator();
		while(iterator.hasNext()){
			key = (String)iterator.next();
			newKey = key.replace(".", "_");
			newArgs.put(newKey, args.get(key));
			hql = hql.replace(":"+key, ":"+newKey);
			countHql = countHql.replace(":"+key, ":"+newKey);
			
			System.out.println("hql: " + hql);
			System.out.println("countHql: " + countHql);
			System.out.println("newArgs: " + newArgs);
		}
		this.pagingQuery(page, hql, countHql, newArgs);
	}
		
	}	

